var embed_div;
var previous_embed;
var myEmbood;

window.onload = function(){
	embed_div = document.getElementById('embedcontainer');
	document.getElementById("id_intro").setAttribute('onkeyup','makeRequest(document.getElementById("id_intro").value)')
}


var handleSuccess = function(o){

	YAHOO.log("The success handler was called.  tId: " + o.tId + ".", "info", "example");
	if(o.responseText == undefined) {
		
	}
	else{
	
		try {
			myEmbood = YAHOO.lang.JSON.parse(o.responseText);
		}
		catch (err) {
			embed_div.innerHTML = 'JSON failed: '+ err + '<br/>tt' + o.responseText;
		}
		if(myEmbood.status=='success' &&
			myEmbood.embedcode != previous_embed) {
			previous_embed=myEmbood.embedcode;
			embed_div.innerHTML = myEmbood.embedcode;
			//div1 = document.getElementById('embedcontainer');
			//div.innerHTML += "<li>Argument object: Object ( [foo] => " + o.argument.foo +
						 " [bar] => " + o.argument.bar +" )</li>";
			document.getElementById("id_link").setAttribute('value',myEmbood.embedcode);
			document.getElementById("emboodle_title").innerHTML=myEmbood.title;
			document.getElementById("id_name").setAttribute('value',myEmbood.title);
		}
		
	}
}

var handleFailure = function(o){

		YAHOO.log("The failure handler was called.  tId: " + o.tId + ".", "info", "example");

	if(o.responseText !== undefined){
		embed_div.innerHTML = "<ul><li>Transaction id: " + o.tId + "</li>";
		embed_div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		embed_div.innerHTML += "<li>Status code message: " + o.statusText + "</li></ul>";
	}
}

var callback =
{
  success:handleSuccess,
  failure:handleFailure,
  argument: { foo:"foo", bar:"bar" }
};

var sUrl = "../mod/emboodle/emboodfetcher.php";

function makeRequest(url){
 
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + "?url=" + url, callback);
	
	YAHOO.log("Initiating request; tId: " + request.tId + ".", "info", "example");
	//document.getElementById("id_link").setAttribute('value',url);

}

